<?php
// 上次订单
class GoodsrefundAction extends UserAction {
	//魔术方法
public function __construct(){
		parent::__construct();
		
	}
	    //订单列表
    public function index()
    {  
      $menuwhere["rec_id"] =  array("eq",$_GET["id"]);
      $data=M("g_order_goods")->where($menuwhere)->find();

      $this->data=$data;
      $where=array();
      $where['order_goods_id']=$data['rec_id'];
      $refund= M("g_order_refund")->where($where)->find();
		$chat_where['type'] =2;
		$chat_where['refund_id'] =$refund['ref_id'];
		M('g_order_refund_chat')->where($chat_where)->setField('is_read','1');
      if($refund){
          if($refund['refund_img']){
              $refund['refund_img']=explode(',', $refund['refund_img']);
          }
          $this->refund=$this->get_refund_status($refund);
          if($refund['refund_status'] == 10){
			  $list=M('g_order_refund_chat')->where('refund_id='.$refund['ref_id'])->select();
			  foreach ($list as $k=>$v){
                  if($v['img']){
                      $list[$k]['chat_img'] = explode(',', $v['img']);
                  }
			  }
			  $where_chat['refund_id']=$refund['ref_id'];
			  $where_chat['type']=2;
			  $last_chat=M('g_order_refund_chat')->where($where_chat)->order('id desc')->getField('id');
			  $this->assign('last_chat',$last_chat);
			  $this->list=$list;
              $this->display('refund_chat');
			  die;
          }else {
			  $this->display('refund_detail');
			  die;
		  }
      }
      $or_where['order_id']=$data['order_id'];
      $or_where['user_del']=0;
      $or_where['user_id']=$this->uid;
      $order = M("g_order_info")->where($or_where)->find();
      if($order){
       $order_show=$this->get_order_status($order);
       $order['order_state']=$order_show['code'];
       $order['order_state_name']=$order_show['name'];
      }
      $this->order=$order;
      $this->display();
    }
   
   //新增退货
   public function refund(){
         $return_data['status']=0;
         $order_id=$_POST['order_id'];
         $order_goods_id=$_POST['order_goods_id'];
         $refund_type=$_POST['refund_type'];
         $refund_img=$_POST['refund_img']?implode(',', $_POST['refund_img']):'';
          $refund_remark=$_POST['refund_remark'];
         $refund_name=$_POST['refund_name'];
         $refund_tel=$_POST['refund_tel'];
         if(empty($refund_remark)){
            $return_data['error']="请填写问题描述";
            echo json_encode($return_data);die;
        }
        if(empty($refund_name)){
            $return_data['error']="请填写联系人";
            echo json_encode($return_data);die;
        }
        if(empty($refund_tel)){
            $return_data['error']="请填写联系电话";
            echo json_encode($return_data);die;
        }
        $where=array();
        $where['order_goods_id']=$order_goods_id;
        $refund= M("g_order_refund")->where($where)->find();
        if(!empty($refund)) {
            $return_data['status']=1;
            echo json_encode($return_data);die;
        }
        $where=array();
        $where["rec_id"] =  array("eq",$order_goods_id);
        $data=M("g_order_goods")->where($where)->find();
        if(empty($data)) {
            $return_data['error']="该订单产品不存在";
            echo json_encode($return_data);die;
        }
/*        if($refund_type){//0换货 1退货
             if(empty($data['is_refund'])) {
              $return_data['error']="该商品不可退货";
              echo json_encode($return_data);die;
             }
        }else{
            if(empty($data['is_exchange'])) {
             $return_data['error']="该商品不可返修/换货";
             echo json_encode($return_data);die;
             }
        }*/
        $or_where=array();
        $or_where['order_id']=$data['order_id'];
        $or_where['user_del']=0;
        $or_where['user_id']=$this->uid;
        $order = M("g_order_info")->where($or_where)->find();
         if(empty($order)) {
            $return_data['error']="该订单不存在";
            echo json_encode($return_data);die;
        }
         $order_show=$this->get_order_status($order);
        if($order_show['code']!='delivered'){//商家已发货可退 确认收货之后不可退货
          $return_data['error']="订单不可退货";
            echo json_encode($return_data);die;
        }
        $add_data=array();
        $add_data['order_id']=$order_id;
        $add_data['order_goods_id']=$order_goods_id;
        $add_data['refund_type']=$refund_type;
        $add_data['refund_img']=$refund_img;
        $add_data['refund_remark']=$refund_remark;
        $add_data['refund_tel']=$refund_tel;
        $add_data['refund_name']=$refund_name;
        $add_data['member_id']=$this->uid;
        $add_data['add_time']=time();
        $res=M('g_order_refund')->add($add_data);
        if($res!==false){
           $log=array();
           $log['order_id']=$order_id;
           $log['refund_id']=$res;
           $log['log_msg']='申请售后';
           $log['log_time']=time();
           $log['log_role']="买家";
           $log['log_orderstate']='0';
			//对话数据
			$chat=array();
            $chat['refund_id'] = $res;
			$chat['member_id'] = $this->uid;
			$chat['pid'] = 0;
			$chat['content'] = $refund_remark;
			$chat['img'] = $refund_img;
			$chat['type'] = 1 ;
			$chat['log_orderstate'] = '0';
			$chat['add_time'] = $log['log_time'];
           M('g_order_refund_chat') ->add($chat);
           M('g_order_refund_log')->add($log);
           $return_data['status']=1;
           echo json_encode($return_data);die;
        }else{
          $return_data['error']="提交失败";
           echo json_encode($return_data);die;
        }
        
   }
   //退货 返修 发货
   public function refund_status(){
         $return_data['status']=0;
         $ref_id=$_POST['ref_id'];
         $invoice_no_name=$_POST['invoice_no_name'];
         $invoice_no_user=$_POST['invoice_no_user'];
         $user_send_img=$_POST['user_send_img'];
         $send_img=$_POST['send_img'];
          $save_data=array();
         if(empty($send_img)){
            if(empty($invoice_no_name)){
            $return_data['error']="请填写快递名称";
            echo json_encode($return_data);die;
            }
            if(empty($invoice_no_user)){
                $return_data['error']="请填写发货单号";
                echo json_encode($return_data);die;
            }
             $save_data['invoice_no_name']=$invoice_no_name;
             $save_data['invoice_no_user']=$invoice_no_user;
             $log_msg='用户已发货,发货单:'.$invoice_no_name.'-'.$invoice_no_user;
         }else{
           if(empty($user_send_img)){
            $return_data['error']="请上传快递单图片";
            echo json_encode($return_data);die;
            }
            $save_data['user_send_img']=$user_send_img;
            $log_msg='用户已发货';
         }
        $where=array();
        $where['ref_id']=$ref_id;
        $where['member_id']=$this->uid;
        $refund= M("g_order_refund")->where($where)->find();
        if($refund['refund_status']!=1) {
            $return_data['error']="退货单状态错误";
            $return_data['dd']=$refund;
            echo json_encode($return_data);die;
        }
        $save_data['refund_status']=2;
        $save_data['user_send_time']=time();
        $where=array();
        $where['ref_id']=$ref_id;
        $where['member_id']=$this->uid;
        $where['refund_status']=1;
        $res=M('g_order_refund')->where($where)->save($save_data);
        if($res!==false){
            $log=array();
           $log['order_id']=$refund['order_id'];
           $log['refund_id']=$ref_id;
           $log['log_msg']=$log_msg;
           $log['log_time']=time();
           $log['log_role']="买家";
           $log['log_orderstate']='2'; 
           M('g_order_refund_log')->add($log);

           $return_data['status']=1;
           echo json_encode($return_data);die;
        }else{
          $return_data['error']="提交失败";
           echo json_encode($return_data);die;
        }
        
   }
    //退货 返修 收货
   public function refund_status1(){
         $return_data['status']=0;
         $ref_id=$_POST['ref_id'];

        $where=array();
        $where['ref_id']=$ref_id;
        $where['member_id']=$this->uid;
        $refund= M("g_order_refund")->where($where)->find();
        if($refund['refund_status']!=4) {
            $return_data['error']="退货单状态错误";
            echo json_encode($return_data);die;
        }
        $save_data=array();
        $save_data['refund_status']=5;
        $save_data['receipt_time']=time();
        $where=array();
        $where['ref_id']=$ref_id;
        $where['member_id']=$this->uid;
        $where['refund_status']=4;
        $res=M('g_order_refund')->where($where)->save($save_data);
        if($res!==false){
            $log=array();
           $log['order_id']=$refund['order_id'];
           $log['refund_id']=$ref_id;
           $log['log_msg']='用户已收货';
           $log['log_time']=time();
           $log['log_role']="买家";
           $log['log_orderstate']='5'; 
           M('g_order_refund_log')->add($log);
           $return_data['status']=1;
           echo json_encode($return_data);die;
        }else{
          $return_data['error']="提交失败";
           echo json_encode($return_data);die;
        }
        
   }
 //退货列表
    public function refund_list()
    {
        $where['ref.member_id'] = $this->uid;
   /*     0 待确认 1已确认 2 用户已发货 
换货单 3商家已收货（待发货）  4商家已发货 5用户已收货(已完成) 
退货单 3商家已收货（待退款）  5已完成
10商户拒绝
*/
       // $where['ref.refund_status'] = array('in','0,1,2,3,4');//完成的不显示
        //$where['user_del']=0;
        $pre=C('DB_PREFIX');//表前缀
        $p = $_REQUEST['p'];
        $pagesize = 10;
        $p = !empty($p) ? $p : 1;
        $start = ($p - 1) * $pagesize;
        $field = "ref.*,ord.order_sn,good.goods_name,good.goods_attr,good.goods_image,good.goods_price,good.goods_number";
        $list = M()->table($pre.'g_order_refund ref')//
        ->join($pre.'g_order_info ord on ord.order_id=ref.order_id')//
        ->join($pre.'g_order_goods good on good.rec_id=ref.order_goods_id')//
        ->where($where)//
        ->field($field)->limit($start, $pagesize)->order('ref.add_time desc')->select();
        foreach ($list as $key => $value) {
            $list[$key]=$this->get_refund_status($value);    
            $list[$key]['add_time'] = date("Y/m/d", $value['add_time']);
            $chat_where['type'] =2;
            $chat_where['is_read'] = 0;
            $chat_where['refund_id'] =$value['ref_id'];
            $list[$key]['un_read']=M('g_order_refund_chat')->where($chat_where)->count();
        }

        if (IS_AJAX) {
            echo json_encode($list);
            die;
        }
        $this->list = $list;
      
        $this->webtitle = "FG峰购";
        $this->display();
    }


	public function refund_chat(){
		$add_chat['pid']=$_POST['chat_id'];
		$add_chat['refund_id'] = $_POST['ref_id'];
		$add_chat['content'] = $_POST['refund_remark'];
		$refund_img=$_POST['refund_img']?implode(',', $_POST['refund_img']):'';
		$add_chat['img'] =$refund_img;
		$add_chat['time'] =time();
		$add_chat['type'] = 1 ;//2，商家 1，买家
		$add_chat['member_id'] = $this->uid;
		$add_chat['log_orderstate'] = M('g_order_refund_chat')->where('id='.$_POST['chat_id'])->getField('log_orderstate');
		$return_data['status']=1;
		$re= M('g_order_refund_chat')->add($add_chat);
		if($re){
			$return_data['status']=1;
			echo json_encode($return_data);die;
			}else{
			$return_data['error']="提交失败";
			echo json_encode($return_data);die;
		}
	}

}